package pl.agh.pp.zephyr.logic;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Umożliwia programowy dostęp do kontekstu Springa
 * */
public class SpringFactory {

	/*
	 * @Bean(autowire = Autowire.BY_TYPE, name="area")
	 * 
	 * @Scope(BeanDefinition.SCOPE_SINGLETON) public H323Area area() { return
	 * new H323Area(); }
	 * 
	 * @Scope(BeanDefinition.SCOPE_PROTOTYPE)
	 * 
	 * @Bean(autowire = Autowire.BY_TYPE, name = "pushLogger",
	 * destroyMethod="destroy") public PushLogger pl() { return new
	 * PushLogger(); }
	 * 
	 * 
	 * @Bean(autowire = Autowire.BY_TYPE, name = "engine")
	 * 
	 * @Scope(BeanDefinition.SCOPE_SINGLETON) public Engine eng() { return new
	 * Engine(getBean(H323Area.class)); }
	 * 
	 * @Bean(autowire = Autowire.BY_TYPE, destroyMethod="destroy")
	 * 
	 * @Scope(BeanDefinition.SCOPE_PROTOTYPE) public GkController gkc() { return
	 * new GkController(); }
	 * 
	 * @Bean(autowire = Autowire.BY_TYPE)
	 * 
	 * @Scope(BeanDefinition.SCOPE_SINGLETON) public RouteRequestFactory rrf() {
	 * return new RouteRequestFactory(getBean(Engine.class)); }
	 * 
	 * @Bean
	 * 
	 * @Scope(BeanDefinition.SCOPE_PROTOTYPE) public GkClient cli() { return new
	 * GkClient(); }
	 */

	private static WebApplicationContext ctx;

	public static <T> T getBean(Class<T> c) {
		if (ctx == null) {

			ctx = WebApplicationContextUtils.
			getRequiredWebApplicationContext((ServletContext) FacesContext
					.getCurrentInstance().getExternalContext().getContext());
			/*
			ctx = new XmlWebApplicationContext();
			ctx.setServletContext((ServletContext) FacesContext
					.getCurrentInstance().getExternalContext().getContext());
			ctx.setConfigLocation("/WEB-INF/spring-context.xml");
			ctx.refresh();
			*/
		}
		
		return ctx.getBean(c);
	}
}
